package org.hzy.test1;


import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hzy.entity.Dept;
import org.hzy.entity.Emp;

public class Examples {

	static Configuration config=new Configuration().configure();
	static SessionFactory fac=config.buildSessionFactory();
	static Session sess=fac.openSession();
	
	public static void test1(){
		Transaction tran=sess.beginTransaction();
		Dept dept=new Dept();
		dept.setDname("JAVA");
		dept.setLoc("JX");
		sess.save(dept);
		tran.commit();
	}
	
	public static void test2(){
		Transaction tran=sess.beginTransaction();
		Dept dept=new Dept();
		dept.setDeptno(18);
		sess.delete(dept);
		tran.commit();
	}
	
	public static void test3(){
		Transaction tran=sess.beginTransaction();
//		Dept dept=(Dept) sess.get(Dept.class, 10);
		
//		Dept dept=(Dept) sess.load(Dept.class, 10);
		
		Dept dept=(Dept) sess.load(Dept.class,18,LockMode.UPGRADE);
//		dept.setDname("bb");
//		sess.flush();
//		tran.commit();
		System.out.println(dept.getDname());
		
	}
	
	public static void test4(){
		Transaction tran=sess.beginTransaction();
//		Dept dept=(Dept) sess.load(Dept.class, 17);
//		sess.update(dept);
//		sess.delete(dept); //只发送两条SQL语句
		
		Dept dept=new Dept();
		dept.setDeptno(18);
		dept.setDname("bb");
		sess.update(dept);
		sess.delete(dept);
		
/*		Dept dept=new Dept();
		dept.setDeptno(18);
		dept.setDname("bb");
		sess.saveOrUpdate(dept);*/
		
//		sess.createSQLQuery("select * from dept;delete from dept where deptno = 7;");
//		sess.merge(dept);
		System.out.println(dept.getDname());
		tran.commit();
		sess.close();
		dept.setDeptno(null);
		Session sess1=fac.openSession();
		Transaction tran1=sess1.beginTransaction();
		sess1.saveOrUpdate(dept);
		tran1.commit();
		sess1.close();
		}
	
	public static void test5(){
		Transaction tran=sess.beginTransaction();
		Dept dept=(Dept) sess.get(Dept.class, 23);
		Emp emp=new Emp();
		emp.setEmpno(1123);
		emp.setEname("aa");
		emp.setDept(dept);
		Emp emp1=new Emp();
		emp1.setEmpno(1124);
		emp1.setEname("bb");
		emp1.setDept(dept);
		sess.save(dept);
		sess.save(emp);
		sess.save(emp1);
		tran.commit();
	}
	
	public static void test6(){
		Transaction tran=sess.beginTransaction();
//		Dept dept=(Dept) sess.get(Dept.class, 23);
		
		Emp emp=(Emp) sess.get(Emp.class, 7369);//  单向多队一 (多的对方为主动方,让一补充多  emp left out join dept)
		
		System.out.println(emp.getEname());
		System.out.println(emp.getDept().getDname());//代理 发送两条
		
		
		tran.commit();
		
		sess.close();
	}
	
	//set
	public static void test7(){
		Transaction tran=sess.beginTransaction();
		
		Emp emp=new Emp();
		emp.setEmpno(1222);
		emp.setEname("XXX");

//		Dept dept=(Dept) sess.get(Dept.class, 29);//单向一对多 dept为主 emp 来补充
		Dept dept=new Dept();
		dept.setDname("aa");
		sess.save(dept);
/*		Set<Emp> emps=dept.getEmps();
		
		emps.add(emp);
		
		System.out.println(emps.size());*/

		tran.commit();
		
		sess.close();
	}
	
	public static void test8(){
		Transaction tran=sess.beginTransaction();
		Dept dept=(Dept) sess.get(Dept.class, 30);
		System.out.println(dept.getEmps().size());
		
/*		Set<Emp> set=dept.getEmps();//装持久化对象的持久化集合(PersistentSet)
		
		Iterator<Emp> it=set.iterator();
		while(it.hasNext()){
			Emp emp =it.next();
			System.out.println(emp.getEname());
		}*/
		
//		List<Emp> list=dept.getEmps();
//		System.out.println(list.size());//<list>节点是有序的 从1开始  连续的    7901 但是<bag>没有
		
//		for (int i = 0; i < list.size(); i++) {
//			if(list.get(i)!=null){
//			System.out.println(list.get(i).getEname());
//			}
//		}
		
//		Map<Integer, Emp> map=dept.getEmps();
//		for (Integer key : map.keySet()) {
//			System.out.println("key:"+key+"\tvalue:"+map.get(key).getEname());
//		}
	
		tran.commit();
		
		sess.close();
	}
	
//getCurrentSession()
	public static void test9(){
		Session se1=fac.getCurrentSession();
		Transaction tran=sess.beginTransaction();
		Dept dept=(Dept) sess.get(Dept.class, 30);
		System.out.println(dept.getDname());
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		test9();

	}

}
